// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
//网页客服 http://web3incubators.com/kefu.html
const {ccclass, property} = cc._decorator;

@ccclass
export default class GyroscopeMgr extends cc.Component {

    static GyoscopeFun:(res)=>void = null;

    /**
     * 开始监听陀螺仪
     * @param GyriFun(res)  res：
     * {
        x	number	x 轴的角速度
        y	number	y 轴的角速度
        z	number	z 轴的角速度
        h5 没有以下属性 需要自己算 
        roll	number	方位角，围绕 z 轴旋转角度
        pitch	number	俯仰角，围绕 x 轴旋转角度
        yaw	number	倾侧角，围绕 y 轴旋转角度} 
     */
    static startGyroscope(GyriFun:(res)=>void)
    {
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {
                window["wx"].startGyroscope(
                    {
                        interval: 'game',
                        success(){console.log("调用陀螺仪成功！");},
                        fail(){console.log("调用陀螺仪失败！");},
                        complete(){console.log("尝试调用陀螺仪！");}
                    })
                    window["wx"].onGyroscopeChange((res)=>
                    {
                        GyriFun&&GyriFun(res);
                    });
                break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                window["tt"].startGyroscope(
                {
                    interval: 20,
                    success(){console.log("调用陀螺仪成功!");},
                    fail(res){console.log(res.errMsg,"调用陀螺仪失败！");},
                    complete(){console.log("尝试调用陀螺仪！");}
                });
                window["tt"].onGyroscopeChange((res)=>
                {
                    GyriFun&&GyriFun(res);
                });
                break;
            }
            case cc.sys.VIVO_GAME: {
                //TODO vivo陀螺仪  vivo没有陀螺仪...
                break;
            }
            case cc.sys.OPPO_GAME: {
                window['qg'].startGyroscope({
                    interval: 'game',
                    success: function(res) { console.log(res);},
                    fail: function(err) { console.log(err);},
                    complete: function() {}
                });
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    //TODO 
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        //TODO 快手陀螺仪  快手没有陀螺仪...
                    } else {
                        this.GyoscopeFun=(res)=>
                        {
                            let data:any = {}
                            data.x = res.alpha;
                            data.y = res.beta;
                            data.z = res.gamma;
                            GyriFun&&GyriFun(data);
                        }
                        window.addEventListener('deviceorientation',this.GyoscopeFun, false);
                    }
                }
                break;
            }
        }
    }
    /**
     * 停用陀螺仪
     */
    static stopGyroscope()
    {
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {
                window["wx"].stopGyroscope(
                {
                    success(){console.log("停用陀螺仪成功！");},
                    fail(){console.log("停用陀螺仪失败！");},
                    complete(){console.log("尝试停用陀螺仪！");}
                });
                window['wx'].offGyroscopeChange();
            break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                window["tt"].stopGyroscope(
                {
                    success(){console.log("停用陀螺仪成功！");},
                    fail(){console.log("停用陀螺仪失败！");},
                    complete(){console.log("尝试停用陀螺仪！");}
                });
                window['tt'].offGyroscopeChange();
            break;
            }
            case cc.sys.VIVO_GAME: {
                
                break;
            }
            case cc.sys.OPPO_GAME: {
                window['qg'].stopGyroscope({
                    success: function(res) { console.log("陀螺仪停用成功",res); },
                    fail: function(err) { console.log("陀螺仪停用失败", err); },
                    complete: function() {console.log("尝试停用陀螺仪！");}
                });
                window['qg'].offGyroscopeChange();
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        
                    } else {
                        window.removeEventListener('deviceorientation',this.GyoscopeFun, false);
                    }
                }
                break;
            }
        }
    }
}
